﻿using Assets.Scripts.DL.Enemy.CommonEnemy;
using System;
using UnityEngine;

namespace Assets.Scripts.DL.Enemy.Wizard {
	public class WizardAtk : CommonState<Controller> {
		readonly Timer _cdTimer;
		readonly Timer _sendDamageGapTimer = new();
		int _sendDmgTimes;


		Vector2 _atkOffset;
		float _range;
		int _damage;
		public WizardAtk(Controller controller, StateEnum stateName) : base(controller, stateName) {
			_cdTimer = _controller.StateData.AtkCdRecoder[StateName];

			AddConditionToSelf(() => _controller.IsPlayerInAtkRange()
			&& _cdTimer.Elapsed > _controller.Config.CommonAtkGap);
		}

		public override void Enter() {
			_controller.PlayAnim(StateName);
			_controller.Velocity = Vector2.zero;
			_controller.StateData.AtkOver = false;
			_atkOffset = _controller.Config.AtkOff;
			_range = _controller.Config.AtkDis;
			_damage = _controller.Config.Damage;
			_controller.CheckAtk(_atkOffset, _range, _damage);
			_sendDamageGapTimer.Reset();
			_sendDmgTimes = 1;
		}

		public override void Update() {
			if (_sendDamageGapTimer.Elapsed > _controller.Config.ConsistentAtkGap) {
				_controller.CheckAtk(_atkOffset, _range, _damage);
				_sendDamageGapTimer.Reset();
				_sendDmgTimes++;
			}
			if (_sendDmgTimes > _controller.Config.ConsistentAtkCnt) {
				_controller.StateData.AtkOver = true;
			}
		}

		public override void Exit() {
			_cdTimer.Reset();
			_controller.StateData.LastAtkTime.Reset();
		}

	}
}
